package org.uim.cricket.analyzer.parsers.batting.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.uim.cricket.analyzer.parsers.batting.AbstractBattingInningsParser;
import org.uim.cricket.analyzer.parsers.batting.BattingInningParsable;
import org.uim.cricket.analyzer.parsers.inning.InningHeaderLabelParsable;
import org.uim.cricket.entities.aggr.SingleInning;
import org.uim.cricket.entities.batting.BattingInning;
import org.uim.cricket.entities.inning.InningLabel;
import org.uim.cricket.util.HTMLStringUtils;
import org.uim.cricket.util.MyStringUtils;

public class BattingInningsParser extends AbstractBattingInningsParser {

    public static String BATTING_HEADER_STARTER = "<tr class=\"inningsHead\">";
    public static String BATTING_INNING_STARTER = "<tr class=\"inningsRow\">";
    
    private InningHeaderLabelParsable innLabelParser = null;
    
    public BattingInningsParser(BattingInningParsable theIBattingInningParsable, InningHeaderLabelParsable theInnLabelParser) {
        super(theIBattingInningParsable);
        this.innLabelParser = theInnLabelParser;
    }

    @Override
    public List<BattingInning> parseAllBattingInnings(StringBuilder sb, SingleInning inning) {
        ArrayList<BattingInning> bats = new ArrayList<BattingInning>();
       
        int pos = sb.indexOf(BATTING_HEADER_STARTER);
        int epos = sb.indexOf("</tr>", pos + BATTING_HEADER_STARTER.length());

        String headerText = sb.substring(pos, epos);
        HashMap<String, Integer> header = this.parseBatInningHeader(headerText, inning);
        
        String[] rows = MyStringUtils.splitInBetween(sb.toString(), BATTING_INNING_STARTER, "</tr>");
        if (rows == null) return bats;
        
        int position = 1;
        for (int i=0; i<rows.length; i++) {
            BattingInning batinn = super.getBattingParsable().parseBattingInning(new StringBuilder(rows[i]), header);
            if (batinn != null) {
            	batinn.setBattedPosition(position++);
                bats.add(batinn);
            }
        }

        return bats;
    }

	private HashMap<String, Integer> parseBatInningHeader(String source, SingleInning inning) {
	    HashMap<String, Integer> header = new HashMap<String, Integer>();
	    if (source == null || source.length() == 0) return header;
	    
	    final int INDEX_OFFSET = 3;
	    String[] rows = MyStringUtils.splitInBetween(source, "<td", "</td>");
	    if (rows.length <= 1) return header;
	    
	    rows[1] = "<" + rows[1];
	    InningLabel innl = this.innLabelParser.parseInnLabel(HTMLStringUtils.cleanHTMLTags(rows[1]));
	    inning.setBattedTeam(innl.getBattedTeam());
	    inning.setMaximumInningOvers(innl.getMaxOvers());
	    inning.setTargetChased(innl.getTarget());
	    inning.setTargetChasedOvers(innl.getTargetChaseOvers());
	    
	    if (rows.length <= 4) return header;
	    int count = 0;
	    
	    for (int i=2;i<rows.length;i++) {
	        String key = HTMLStringUtils.cleanHTMLTags("<" + rows[i]);
	        
	        if (key.trim().length() > 0) {
	            header.put(key, INDEX_OFFSET + count);
	        }
	        count++;
	    }
	    
	    return header;
	}
	
	public void setInnLabelParser(InningHeaderLabelParsable innLabelParser) {
        this.innLabelParser = innLabelParser;
    }
	
	public InningHeaderLabelParsable getInnLabelParser() {
        return innLabelParser;
    }
}
